package com.smart.sso.demo.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

import javax.activation.MailcapCommandMap;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//import com.para.osc.framework.common.Application;
//import com.para.osc.framework.security.view.UserAuthInfo;
//import com.paraview.base.jar.core.tools.AppConfig;

@Configuration
@Slf4j
public class OAuthAuthenticationFilter implements Filter{
    public static final String client_ID = "c0JZOU3K5d"; //应用注册ID
    public static final String redirect_uri = "http://10.0.0.102:8084/back";

    public void init(FilterConfig config) {

    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException{
        HttpServletRequest httpRequest = (HttpServletRequest)request;
        HttpServletResponse httpResponse = (HttpServletResponse)response;
        if (checkFilteredUrl(httpRequest, httpResponse)) {
            chain.doFilter(httpRequest, httpResponse);
            return;
        }
        if (httpRequest.getSession().getAttribute("uid")!=null){
            return;
        }else {
            log.info("-------targetUri--------------"+httpRequest.getRequestURL());
            httpResponse.sendRedirect("https://ssotst.smedi.com:8443/profile/oauth2/authorize?client_id="+client_ID+"&redirect_uri="+redirect_uri+"&response_type=code&target_uri="+httpRequest.getRequestURL());
        }

        log.info("------------------continue------------------");
//        UserAuthInfo userInfo = Application.getUserAuthInfo();
//        if (userInfo != null) {
//            chain.doFilter(httpRequest, httpResponse);
//            return;
//        }
//        String oauthOpen = AppConfig.getParamValue("com.config.oauth.open");
//        if (Boolean.valueOf(oauthOpen)) {
//            httpRequest.getRequestDispatcher("/oauth_authentication").forward(httpRequest, httpResponse);
//            return;
//        }
//        else {
//            chain.doFilter(httpRequest, httpResponse);
//            return;
//        }
    }

    public static void main(String[] args) {
        String str="/back";
        System.out.println(str.indexOf("/back"));
    }

    private boolean checkFilteredUrl(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String url = request.getRequestURI();
        log.info("---------------------url-------------------"+url);
        if (
                url.indexOf("oauth_callback") > -1 ||
                        url.indexOf("oauth_error") > -1 ||
                        url.indexOf("/js") > -1 ||
                        url.indexOf("/css") > -1 ||
                        url.indexOf("/images") > -1 ||
                        url.indexOf("/favicon") > -1||
                        url.indexOf("/back") > -1||
                        url.indexOf("/back1") > -1) {
            return true;
        }
        return false;
    }

    public void destroy() {

    }

}
